/*
    This file is part of Raptor.

    Raptor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Raptor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Raptor.  If not, see <http://www.gnu.org/licenses/>.

 */

#ifndef HOSTER_H
#define HOSTER_H

#include "../../../include/interfaceHoster.h"

#include <QtGui>
#include <QObject>
#include <QSettings>

class pluginHoster : public QObject, InterfaceHoster
{
    Q_OBJECT
    Q_INTERFACES(InterfaceHoster)

    public:

        pluginHoster();
        virtual ~pluginHoster();

        virtual QObject* clone();

        virtual void start();
        virtual void stop();
        virtual void login(QString const& user, QString const& pass);
        virtual void getSize();

        virtual bool setLink(QUrl const& url);
        virtual void createConfigDialog(QPointer<QGridLayout> hosterLayout);
        virtual void deleteConfigDialog();

    signals:

        void loginFinished(int const& id, bool const& error);
        void getSizeFinished(int const& id, bool const& error);
        void downloadFinished(int const& id, bool const& error);
        void deleteMe(int const& id, bool const& error);
        void statusMsg(int const& id, QString const& msg);
        void changeMode(int const& id, InterfaceHoster::E_MODE const& mode);
        void changeName(int const& id, QString const& name);
        void messageBox(QString const& title, QString const& text);

    private slots:

        void slot_http_requestFinished(int id, bool error);
        void slot_http_readyRead(const QHttpResponseHeader & resp);
        void slot_http_dataReadProgress(int done, int total);
        void slot_http_sslErrors(const QList<QSslError> & errors);

        void slot_speedTimer_timeout();
        void slot_waitTimer_timeout();

        //config dialog
        void slot_applyButton_clicked();
        void slot_authLoginButton_clicked();
        void slot_authDeleteButton_clicked();
        void slot_raptor_hosterLoginFinished(int const& id, bool const& error);

    private:

        enum E_ACTION
        {
            ACTION_NONE,
            ACTION_START,
            ACTION_DOWNLOAD,
            ACTION_API
        };

        int step;
        int multipl;
        E_ACTION action;
        bool abort;

        QByteArray response;
        QString page;
        QString cookie;

        int waitingId;

        //timer
        QTimer waitTimer;
        int wait;
        int timerStep;
        ///timer


        //api
        QString apiFilename;
        quint64 apiFileId;
        QString apiHostname;
        QString apiAuth;
        int apiWait;
        QString apiMd5;
        ///api

        //speed
        QTimer speedTimer;
        QList<double> speedList;
        quint64 lastSize;
        ///speed

        //config dialog
        QPointer<QPushButton> applyButton;
        QPointer<QLabel> authLabel;
        QPointer<QLineEdit> authUser;
        QPointer<QLineEdit> authPass;
        QPointer<QPushButton> authLogin;
        QPointer<QPushButton> authDelete;

        QPointer<QLabel> maxDownloadsLabel;
        QPointer<QSpinBox> maxDownloadsSpinBox;

        bool checkRsResponse();

        int request(QUrl const& url, QString const& method, QByteArray const& post, QMap<QString, QString> const& headerData, QIODevice *io=0);

        bool extractResponse(QString const& field, QString & value);
        QString selectMirror(QStringList const& list);
        void setSpeed(double const& newSpeed);

        void loadCookie();
        void setError();

        void doGetSize();
        void doLogin();
        void doPremium();
        void doFree();
};

#endif // HOSTER_H
